/** 资产盘点计划 */

import React, {useEffect, useState} from 'react'
import {Button, Space, message} from 'antd'
import type {ColumnsType} from 'antd/es/table'
import {history, connect} from 'umi'
import {PlusOutlined, ArrowDownOutlined} from '@ant-design/icons'
import moment from 'moment'
import styles from './index.less'
import SearchForm from '@/components/SearchForm'
import Permissions from '@/components/Permissions'
import CustomTable from '@/components/Table'
import {getPlanList} from '@/service/storage/assetInventoryPlan'
import {downloadDocumentUrl} from '@/service/common/download'

interface DataType {
	stocktakeType: any
	isAudit: number
	auditStatus: number
	planId: string | string[] | null
	personCount: any
	successPersonCount: any
	startUpStatus: number
	id: string
	supplierCode: string
	supplierName: string
	serviceType: string
	contacts: string
	status: string
	date: string
}

const formItemLayout = {
	labelCol: {flex: '120px'},
	wrapperCol: {flex: '1'},
}

const archives = () => {
	const [dataSource, setDataSource] = useState<any[]>([])
	const [searchData, setSearchData] = useState({})
	const [loading, setLoading] = useState(false)
	const [total, setTotal] = useState()
	const [params, setParams] = useState({
		current: 1,
		size: 10,
	})

	// 获取盘点计划列表
	const getData = () => {
		setLoading(true)
		getPlanList({...params, ...searchData})
			.then((res: any) => {
				if (res?.success) {
					setDataSource(res?.data?.records)
					setTotal(res?.data?.total)
					setLoading(false)
				} else {
					setLoading(false)
				}
			})
			.catch((err) => {
				setDataSource([])
				message.error(err)
				setLoading(false)
			})
	}
	useEffect(() => {
		getData()
	}, [params])

	/** 点击详情及编辑事件 */
	const onDetails = (type: string, record: DataType) => {
		const queryData: any = {
			planId: record?.planId,
		}
		if (type === 'inventoryList') {
			queryData.stocktakeType = record.stocktakeType
		}
		history.push({
			pathname: `/storage/assetInventory/assetInventoryPlan/${type}`,
			query: queryData,
		})
	}

	const columns: ColumnsType<DataType> = [
		{
			title: 'No',
			dataIndex: 'warehouseCode',
			align: 'center',
			width: 60,
			defaultSortOrder: 'ascend',
			render(text, record, index) {
				return <span>{(params.current - 1) * 10 + index + 1}</span>
			},
		},
		{
			title: '盘点计划编号',
			dataIndex: 'planId',
			align: 'center',
			width: 150,
		},
		{
			title: '审核状态',
			dataIndex: 'auditStatus',
			align: 'center',
			width: 100,
			render: (text) => {
				if (text === 0) {
					return '待审核'
				}
				if (text === 1) {
					return '审核中'
				}
				if (text === 2) {
					return '审核通过'
				}
				return '审核拒绝'
			},
		},
		{
			title: '盘点计划名称',
			dataIndex: 'name',
			align: 'center',
			width: 150,
		},
		{
			title: '负责人',
			width: 100,
			dataIndex: 'chargePerson',
			align: 'center',
		},
		{
			title: '计划状态',
			width: 100,
			dataIndex: 'status',
			align: 'center',
			render: (text) => {
				if (text === 0) {
					return '待开始'
				}
				if (text === 1) {
					return '进行中'
				}
				if (text === 2) {
					return '已完成'
				}
				return '已逾期'
			},
		},
		{
			title: '计划开始时间',
			dataIndex: 'beginTime',
			align: 'center',
			width: 120,
			render: (text) => moment(text)?.format('YYYY-MM-DD'),
		},
		{
			title: '计划结束时间',
			dataIndex: 'endTime',
			align: 'center',
			width: 120,
			render: (text) => moment(text)?.format('YYYY-MM-DD'),
		},
		{
			title: '盘点类型',
			dataIndex: 'stocktakeType',
			align: 'center',
			width: 150,
			render: (text) => <>{text === 1 ? '全盘' : '抽盘'}</>,
		},
		{
			title: '参与人',
			dataIndex: 'personCount',
			align: 'center',
			width: 100,
		},
		{
			title: '完成情况',
			dataIndex: 'createTime',
			align: 'center',
			width: 240,
			render: (text: any, record) => (
				<>
					已完成：{record?.successPersonCount}人，未完成：
					{record.personCount - record.successPersonCount}人
				</>
			),
		},
		{
			title: '创建人',
			dataIndex: 'createUser',
			align: 'center',
			width: 100,
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align: 'center',
			width: 160,
		},
		{
			title: '操作',
			key: 'action',
			align: 'center',
			fixed: 'right',
			width: 260,
			render: (text: any, record) => (
				<Space size={12}>
					<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_view">
						<Button
							type="link"
							onClick={() => {
								onDetails('view', record)
							}}>
							详情
						</Button>
					</Permissions>
					{record?.auditStatus === 0 || record?.auditStatus === 3 ? (
						<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_edit">
							<Button
								type="link"
								onClick={() => {
									onDetails('edit', record)
								}}>
								编辑
							</Button>
						</Permissions>
					) : (
						''
					)}
					{record?.auditStatus === 2 ? (
						<>
							<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_taskFollowUp">
								<Button
									type="link"
									onClick={() => {
										onDetails('taskFollowUp', record)
									}}>
									任务跟进
								</Button>
							</Permissions>
							<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_inventoryList">
								<Button
									type="link"
									onClick={() => {
										onDetails('inventoryList', record)
									}}>
									盘点清单
								</Button>
							</Permissions>
						</>
					) : (
						''
					)}
					{record?.isAudit === 1 ? (
						<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_examine">
							<Button
								type="link"
								onClick={() => {
									onDetails('process', record)
								}}>
								审核
							</Button>
						</Permissions>
					) : (
						''
					)}
				</Space>
			),
		},
	]
	const formItems: any[] = [
		{
			type: 'group',
			key: 'group1',
			groupItems: [
				{
					type: 'input',
					label: '盘点计划编号',
					name: 'planId',
					key: 'planId',
				},
				{
					type: 'input',
					label: '盘点计划名称',
					name: 'name',
					key: 'name',
				},
				{
					type: 'select',
					key: 'status',
					name: 'status',
					label: '盘点计划状态',
					keys: 'value',
					title: 'text',
					items: [
						{
							value: 0,
							text: '待开始',
						},
						{
							value: 1,
							text: '进行中',
						},
						{
							value: 2,
							text: '已完成',
						},
						{
							value: 3,
							text: '已逾期',
						},
					],
				},
			],
		},
		{
			type: 'group',
			key: 'group2',
			groupItems: [
				{
					type: 'select',
					key: 'auditStatus',
					name: 'auditStatus',
					label: '审核类型',
					keys: 'value',
					title: 'text',
					items: [
						{
							value: 0,
							text: '待审核',
						},
						{
							value: 1,
							text: '审核中',
						},
						{
							value: 2,
							text: '审核通过',
						},
						{
							value: 3,
							text: '审核拒绝',
						},
					],
				},
				{
					type: 'select',
					key: 'stocktakeType',
					name: 'stocktakeType',
					label: '盘点类型',
					keys: 'value',
					title: 'text',
					items: [
						{
							value: 1,
							text: '全盘',
						},
						{
							value: 2,
							text: '抽盘',
						},
					],
				},
				{
					type: 'input',
					label: '计划负责人',
					name: 'chargePerson',
					key: 'chargePerson',
				},
			],
		},
		{
			type: 'group',
			key: 'group3',
			groupItems: [
				{
					type: 'input',
					label: '创建人',
					name: 'CreateUserName',
					key: 'CreateUserName',
				},
				{
					type: 'dateRange',
					label: '创建时间',
					name: 'createTime',
					key: 'createTime',
				},
				{},
			],
		},
	]
	const onFinish = (values: any) => {
		const newValue = {...values}
		const searchDatas = {
			...newValue,
			createBeginTime: newValue?.createTime
				? moment(newValue?.createTime?.[0]).format(
						'YYYY-MM-DD 00:00:00',
				  )
				: '',
			createEndTime: newValue?.createTime
				? moment(newValue?.createTime?.[1]).format(
						'YYYY-MM-DD 23:59:59',
				  )
				: '',
		}
		delete searchDatas.createTime
		setSearchData(searchDatas)
		setParams({
			current: 1,
			size: 10,
		})
	}

	const onReset = () => {
		setSearchData({})
		setParams({
			current: 1,
			size: 10,
		})
	}

	return (
		<div className={styles.assetInventoryPlan}>
			<div className={styles.content}>
				<div className={styles.search}>
					<SearchForm
						onFinish={onFinish}
						onReset={onReset}
						formItems={formItems}
						formLayout={formItemLayout}
					/>
				</div>
				<div className={styles.btnlist}>
					<Space size={12}>
						<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_add">
							<Button
								icon={<PlusOutlined />}
								type="primary"
								ghost
								size="small"
								onClick={() => {
									history.push(
										'/storage/assetInventory/assetInventoryPlan/add',
									)
								}}>
								新增数据
							</Button>
						</Permissions>
						<Permissions permissionID="amc_storage_assetInventory_assetInventoryPlan_export">
							<Button
								icon={<ArrowDownOutlined />}
								className={styles.import}
								ghost
								size="small"
								onClick={() => {
									downloadDocumentUrl({
										resourceId: '192',
										queryDesc:
											'盘点计划导出,resourceId:192',
										queryParam: searchData,
									})
								}}>
								导出
							</Button>
						</Permissions>
					</Space>
				</div>
				<CustomTable
					params={params}
					columns={columns}
					total={total}
					pagination
					callback={(pagination: any) => {
						setParams(pagination)
					}}
					loading={loading}
					dataSource={dataSource}
				/>
			</div>
		</div>
	)
}
export default connect(({user}: any) => ({user}))(archives)
